﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAL.Entities;
using BAL;

namespace GrouponDesktop.AbmRol
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            //Traemos el listado de roles
            RolBAL proxy = new RolBAL();

            this.gridRoles.CellContentClick += new DataGridViewCellEventHandler(btnClick);
            gridRoles.DataSource = proxy.GetRoles();
            
        }
        
        public void btnClick(object sender, DataGridViewCellEventArgs e) {     
            int selectedRowIndex = int.Parse(e.RowIndex.ToString());      
            if (this.gridRoles.Columns[e.ColumnIndex].Name.ToString() == "btnDelete"){
                DAL.Entities.RolEntity item = (DAL.Entities.RolEntity)gridRoles.CurrentRow.DataBoundItem;
                if (DeleteRol(item))
                {
                    RolBAL proxy = new RolBAL();
                    gridRoles.DataSource = proxy.GetRoles();
                }
            }
            else if (this.gridRoles.Columns[e.ColumnIndex].Name.ToString() == "btnEdit")
            {
                DAL.Entities.RolEntity item = (DAL.Entities.RolEntity)gridRoles.CurrentRow.DataBoundItem;
                AgregarOEditarRol form = new AgregarOEditarRol(item.IntIdRol);
                form.FormClosed += new FormClosedEventHandler(RefreshGrid);
                form.ShowDialog();
            } 
        }
         
        /// <summary>
        /// Funcion que borra logicamente un rol
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        private bool DeleteRol(DAL.Entities.RolEntity item)
        {
            if (MessageBox.Show("Esta seguro que desea borrar el rol " + item.VarNombre + "?", "Confirm delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                //Borramos el rol
                BAL.RolBAL proxy = new RolBAL();
                proxy.DeleteRol(item);
                return true;
            }else{
                return false;
            }
        }

        /// <summary>
        /// Funcion q abre la ventana de Agregar Rol
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, EventArgs e)
        {
            AgregarOEditarRol form = new AgregarOEditarRol(0);
            form.FormClosed += new FormClosedEventHandler(RefreshGrid);
            form.ShowDialog();
            
        }

        /// <summary>
        /// Funcion que vuelve a cargar la lista
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RefreshGrid(object sender, FormClosedEventArgs e)
        {
            RolBAL proxy = new RolBAL();
            gridRoles.DataSource = proxy.GetRoles();
        }
    }
}
